package com.hemaapp.yjnh.activity;

import xtom.frame.XtomActivityManager;
import xtom.frame.util.XtomSharedPreferencesUtil;
import xtom.frame.util.XtomToastUtil;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaArrayResult;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.BaseUtil;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.bean.User;

/**
 * 找回密码第二步
 */
public class GetPassword1Activity extends BaseActivity {
	private TextView titleText;
	private ImageButton titleLeft;
	private Button titleRight;

	private EditText passwordEditText;
	private EditText repeatEditText;
	private Button nextButton;

	private String username;
	private String tempToken;
	private String password;
	private String keytype = "1";//1：登陆密码；2：支付密码

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_getpassword1);
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void callBeforeDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case PASSWORD_RESET:
			showProgressDialog("请稍候");
			break;
		default:
			break;
		}
	}

	@Override
	protected void callAfterDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case PASSWORD_RESET:
			cancelProgressDialog();
			break;
		default:
			break;
		}
	}

	@Override
	protected void callBackForServerSuccess(HemaNetTask netTask, HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
            case PASSWORD_RESET:
                XtomToastUtil.showShortToast(mContext, "密码重设成功");
				if (keytype.equals("1"))
                	getNetWorker().clientLogin(username, password);
				else {
					finish();
				}
                break;
            case CLIENT_LOGIN:
                HemaArrayResult<User> uResult = (HemaArrayResult<User>) baseResult;
                User user = uResult.getObjects().get(0);
                getApplicationContext().setUser(user);
                String username = netTask.getParams().get("username");
                String password = netTask.getParams().get("password");
                XtomSharedPreferencesUtil.save(mContext, "username", username);
                XtomSharedPreferencesUtil.save(mContext, "password", password);
                XtomActivityManager.finishAll();
                Intent it = new Intent(mContext, MainActivity.class);
                startActivity(it);
                break;
        }

	}

	@Override
	protected void callBackForServerFailed(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
            case PASSWORD_RESET:
                XtomToastUtil.showShortToast(mContext, "密码重设失败，" + baseResult.getMsg());
                break;
            case CLIENT_LOGIN:
                XtomActivityManager.finishAll();
                BaseUtil.checkLogin(mContext);
                break;
        }
    }

	@Override
	protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
            case PASSWORD_RESET:
                XtomToastUtil.showShortToast(mContext, "密码重设失败，请稍后再试");
                break;
            case CLIENT_LOGIN:
                XtomActivityManager.finishAll();
                BaseUtil.checkLogin(mContext);
                break;
        }
    }

	@Override
	protected void findView() {
		titleText = (TextView) findViewById(R.id.title_text);
		titleLeft = (ImageButton) findViewById(R.id.title_left_btn);
		titleRight = (Button) findViewById(R.id.title_right_btn);

		passwordEditText = (EditText) findViewById(R.id.password);
		repeatEditText = (EditText) findViewById(R.id.repeat);
		nextButton = (Button) findViewById(R.id.next);
	}

	@Override
	protected void getExras() {
		username = mIntent.getStringExtra("username");
		tempToken = mIntent.getStringExtra("tempToken");
		keytype = mIntent.getStringExtra("keytype");
	}

	@Override
	protected void setListener() {
		if (keytype.equals("1"))
			titleText.setText("重设登录密码");
		else if (keytype.equals("2"))
			titleText.setText("重设支付/提现密码");
		titleLeft.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		titleRight.setVisibility(View.GONE);
		nextButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				password = passwordEditText.getText().toString();
				String repeat = repeatEditText.getText().toString();
				if (password.length()<6 || password.length()>16) {
					XtomToastUtil.showShortToast(mContext, "密码输入不正确，请输入6-16位密码");
					return;
				}
				if (!password.equals(repeat)) {
					XtomToastUtil.showShortToast(mContext, "密码输入不一致，请重新输入!");
					return;
				}
				getNetWorker().passwordReset(tempToken, password, keytype);
			}
		});
	}
}
